<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 资讯接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (帮助中心)
     * @ApiSummary  (帮助中心)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/index)
     * @ApiParams   (name="tid", type="varchar", required=false, description="分类id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function index()
    {
        $tid = input('tid');
        $cate = Db::name('help_type')->field('id,name')->where('status','normal')->select();
        if (empty($tid)) {
            $help = Db::name('help')->field('id,title')->where('status','normal')->order('createtime desc')->select();
        } else {
            $help = Db::name('help')->field('id,title')->where('status','normal')->where('tid',$tid)->order('createtime desc')->select();
        }
        $data['cate'] = $cate;
        $data['help'] = $help;
        $this->success('ok',$data);
    }

    /**
     * @ApiTitle    (帮助中心搜索)
     * @ApiSummary  (帮助中心搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/search)
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键字")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function search()
    {
        $keyword = input('keyword');
        $res = Db::name('help')
            ->field('id,title,content,createtime')
            ->where('status','normal')
            ->where('title','like','%' . $keyword . '%')
            ->where('content','like','%' . $keyword . '%')
            ->order('createtime desc')
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                $item['createtime'] = date('Y-m-d',$item['createtime']);
                return $item;
            });
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (帮助详情)
     * @ApiSummary  (帮助详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/articleInfo)
     * @ApiParams   (name="id", type="varchar", required=false, description="咨讯id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function articleInfo()
    {
        $id = $this->request->post('id');
        $res = Db::name('help')->where('id',$id)->find();
        $res['content'] = str_replace('/uploads',config('api_image_url').'/uploads',$res['content']);
        $res['createtime'] = date('Y-m-d',$res['createtime']);
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (咨询中心)
     * @ApiSummary  (咨询中心)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/help)
     * @ApiParams   (name="tid", type="varchar", required=false, description="分类id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function help()
    {
        $tid = input('tid');
        $cate = Db::name('article_type')->where('status','normal')->select();
        if (empty($tid)) {
            $article = Db::name('article')
                ->field('id,title,content,createtime')
                ->where('status','normal')
                ->order('createtime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['createtime'] = date('Y-m-d',$item['createtime']);
                    $item['content'] = str_replace('/uploads',config('api_image_url').'/uploads',$item['content']);
                    return $item;
                });

        } else {
            $article = Db::name('article')
                ->field('id,title,content,createtime')
                ->where('status','normal')
                ->where('tid',$tid)
                ->order('createtime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['createtime'] = date('Y-m-d',$item['createtime']);
                    $item['content'] = str_replace('/uploads',config('api_image_url').'/uploads',$item['content']);
                    return $item;
                });
        }

        $data['cate'] = $cate;
        $data['article'] = $article;
        $this->success('ok',$data);
    }

    /**
     * @ApiTitle    (资讯详情)
     * @ApiSummary  (资讯详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/helpInfo)
     * @ApiParams   (name="id", type="varchar", required=false, description="咨讯id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function helpInfo()
    {
        $id = input('id');
        $res = Db::name('article')->where('id',$id)->find();
//        $res['content'] = str_replace('/uploads',config('api_image_url').'/uploads',$res['content']);
        $res['createtime'] = date('Y-m-d',$res['createtime']);
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (资讯中心搜索)
     * @ApiSummary  (资讯中心搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Article/helpSearch)
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键字")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function helpSearch()
    {
        $keyword = input('keyword');
        $res = Db::name('article')
            ->field('id,title,content,createtime')
            ->where('status','normal')
            ->where('title','like','%' . $keyword . '%')
            ->where('content','like','%' . $keyword . '%')
            ->order('createtime desc')
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                $item['createtime'] = date('Y-m-d',$item['createtime']);
                return $item;
            });
        $this->success('ok',$res);
    }


}

